package jia.tools;

import jia.game.GenericFlags;
import org.apache.log4j.Logger;
import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;

import java.io.File;

/**
 * @author Erik Stell
 * @version 1.0
 */
public class LandmarkNameGenerator {
    private static final Logger log = Logger.getLogger(LandmarkNameGenerator.class);
    private static File fileToBeParsed = new File("src/com/gryffonx/xml/landmarks.xml");
    private static SAXReader reader = new SAXReader();
    private static Document xmlDoc = null;
    
    /**
     * @param howMany
     * @return
     */
    public static String generateName(int howMany) {
        if (howMany <= 0) {
            howMany = 1;
        }
        
        try {
            xmlDoc = reader.read(fileToBeParsed);
        } catch (DocumentException e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        
        StringBuffer retVal = new StringBuffer();
        
        
        
        //determine the pattern that will be used.
        int whatPattern = determinePattern();
        
        for (int i = 0; i < howMany; i++) {
            StringBuffer adjVal = new StringBuffer();
            
            if (whatPattern == 1){
                adjVal.append(generateGroup1());
            }else if (whatPattern == 2){
                adjVal.append(generateGroup2());
            }else if (whatPattern == 3){
                adjVal.append(generateGroup3());
            }else if (whatPattern == 4){
                adjVal.append(generateGroup4());
            }else if (whatPattern == 5){
                adjVal.append(generateGroup5());
            } else if (whatPattern == 6){
                adjVal.append(generateGroup6());
            }
            //assign the retVal to the name generated
            retVal.append(adjVal);
            //retVal.append("\n");
        }
        
        return retVal.toString();
    }
    
    private static int determinePattern(){
        return StaticDiceRoller.rolldSomething(6, 1);
    }
    
    /**
     * The GROUP1 GROUP2
     * i.e., The Burning Sands
     * @return
     */
    private static String generateGroup1(){
        StringBuffer retVal = new StringBuffer();
        try {
            retVal.append("The ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 1)));
            retVal.append(" ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 2)));
            
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        return retVal.toString();
    }
    
    /**
     * The GROUP2 of the GROUP1
     * i.e., The Mountains of the Dead
     * @return
     */
    private static String generateGroup2(){
        StringBuffer retVal = new StringBuffer();
        try {
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 2)));
            retVal.append(" of ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 1)));
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        return retVal.toString();
    }
    
    /**
     * NAME's GROUP2
     * i.e., Emond's Field
     * @return
     */
    private static String generateGroup3(){
        StringBuffer retVal = new StringBuffer();
        try {
            
            int whichOne = StaticDiceRoller.rolldSomething(7, 1);
            String gender = determineGender();
            
            if (whichOne == 1){
                //human
                retVal.append(HumanNameGenerator.generateName());
            }else if (whichOne == 2){
                //draconic
                retVal.append(DraconicNameGenerator.generateName(gender, 1));
                
            }else if (whichOne == 3){
                //dwarf
                retVal.append(DwarfNameGenerator.generateName(gender ,1));
                
            }else if (whichOne == 4){
                //elf
                retVal.append(ElfNameGenerator.generateName(gender, 1));
                
            }else if (whichOne == 5){
                //empyric
                retVal.append(EmpyrealNameGenerator.generateName(gender, 1));
                
            }else if (whichOne == 6){
                //fae
                retVal.append(FaeryNameGenerator.generateName(gender, 1));
                
            }else if (whichOne == 7){
                //orc
                retVal.append(OrcNameGenerator.generateName(gender, 1));
                
            }
            //run a random pattern to decide whether to use a humn, dwarf, elf or Empyric name
            
            retVal.append("'s ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 2)));
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        return retVal.toString();
    }
    
    /**
     * The POSSESSION2 of the NATION
     * i.e., The Mountains of the Dead
     * @return
     */
    private static String generateGroup4(){
        StringBuffer retVal = new StringBuffer();
        try {
            retVal.append("The ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 2)));
            retVal.append(" of ");
            //nation
            retVal.append(StringUtils.capitalize(NationNameGenerator.generateName(1)));
            
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        return retVal.toString();
    }
    
    /**
     * The POSSESSION2 of the NATION
     * i.e., The Mountains of Mist
     * @return
     */
    private static String generateGroup6(){
        StringBuffer retVal = new StringBuffer();
        try {
            retVal.append("The ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 2)));
            retVal.append(" of ");
            //nation
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 5)));
            
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        return retVal.toString();
    }
    
    /**
     * The NAME's POSSESSION1
     * i.e., The Mountains of the Dead
     * @return
     */
    private static String generateGroup5(){
        StringBuffer retVal = new StringBuffer();
        try {
            
            int whichOne = StaticDiceRoller.rolldSomething(7, 1);
            String gender = determineGender();
            
            if (whichOne == 1){
                //human
                retVal.append(HumanNameGenerator.generateName());
            }else if (whichOne == 2){
                //draconic
                retVal.append(DraconicNameGenerator.generateName(gender, 1));
                
            }else if (whichOne == 3){
                //dwarf
                retVal.append(DwarfNameGenerator.generateName(gender ,1));
                
            }else if (whichOne == 4){
                //elf
                retVal.append(ElfNameGenerator.generateName(gender, 1));
                
            }else if (whichOne == 5){
                //empyric
                retVal.append(EmpyrealNameGenerator.generateName(gender, 1));
                
            }else if (whichOne == 6){
                //fae
                retVal.append(FaeryNameGenerator.generateName(gender, 1));
                
            }else if (whichOne == 7){
                //orc
                retVal.append(OrcNameGenerator.generateName(gender, 1));
                
            }
            //run a random pattern to decide whether to use a humn, dwarf, elf or Empyric name
            
            retVal.append("'s ");
            retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, 2)));
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        return retVal.toString();
    }
    
    private static String determineGender(){
        String retVal = "";
        
        int flip = StaticDiceRoller.rolldSomething(2, 1);
        
        if (flip == 1){
            retVal = "male";
        }else{
            retVal = "female";
        }
        
        return retVal;
    }
    
}
